from django.db import models
from tagging.models import Tag
from common.models import NameSlugModel, DateAwareModel, MyImageModel, UserOwnedModel
from django.contrib.gis.db import models

class Place(NameSlugModel, DateAwareModel):
    geometry = models.PointField(srid=4326)
    objects = models.GeoManager()
	
    def __unicode__(self):
        return self.name

class Dish(NameSlugModel, DateAwareModel):
    place = models.ForeignKey(Place)
    # Dish can be a specific food or a combination of foods
    # where each food has its own tag.
    tags = models.ManyToManyField(Tag, null=True, blank=True)
    geometry = models.PointField(srid=4326, blank=True, editable=False)
    objects = models.GeoManager()
	
	# Always set geometry equal to geometry of corresponding place.
	# This is to get around not being able to "order by" distance if model 
	# does not contain a PointField.
    def save(self, *args, **kwargs):
        self.geometry = self.place.geometry
        super(Dish, self).save(*args, **kwargs)
				
    def order_by_distance(dish_list, pnt):
        place_list = [d.place for d in dish_list]
        
    def __unicode__(self):
        return self.name

class PlaceImage(MyImageModel, UserOwnedModel):
    place = models.ForeignKey(Place)

class DishImage(MyImageModel, UserOwnedModel):
    dish = models.ForeignKey(Dish)

class Review(DateAwareModel, UserOwnedModel):
    text = models.TextField(blank=True)
    dish = models.ForeignKey(Dish)
    rating = models.IntegerField(blank=True, null=True)
    tags = models.ManyToManyField(Tag, null=True, blank=True)

    def __unicode__(self):
        return str(self.created_date) + ' ' + str(self.dish) + ' by ' + str(self.user)